package cn.jdy.plugins.web.gajjb;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.authority.entity.User;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.xysgajM.Reports;
import cn.jdy.plugins.service.xysgajM.ReportsService;

/**
 * @desc    襄阳市公安举报系统-我要举报操作
 * @author  ningqian
 * @date    2016-06-23
 */
@Controller
@RequestMapping("/iReports")
public class ReportsCtrl extends BaseCtrl {
  @Autowired
  private ReportsService reportsService;
  @Autowired
  private ConfService confService;

	//默认列表路径
	private static String viewName = "redirect:/reports/Succes.jsp";
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		ModelAndView mv = new ModelAndView("/reports.jsp");
		return mv;
	}
	
	
	/**
	 * 保存（更新）举报内容
	 * @param Reports
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(Reports reports, Long pid) {
		ModelAndView mv = new ModelAndView("/Succes.jsp");
		//
		try {
		
			if(reports.getId()==null) {// 新增举报内容
				//
//				User user = getUser();
//				Reports.setUserid(user.getId());
//				Reports.setUsername(user.getUsername());
				
				reports.setAudit1Status("0");
				reports.setAudit2Status("0");
				reports.setReportsTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));//加入举报时间
				//市局需要手动更改时间，如果为空则取正确时间
				if(reports.getFalseCreateTime() == null || reports.getFalseCreateTime() == ""){
					reports.setFalseCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				}
//				Reports.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.reportsService.save(reports);
			//	mv.setViewName(this.viewName.concat("1"));
			} else {//更新举报内容
				User user = getUser();
				reports.setUserid(user.getId());
				reports.setUsername(user.getUsername());
				reports.setReportsTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));//加入举报时间
				if(reports.getFalseCreateTime() == null || reports.getFalseCreateTime() == ""){
					reports.setFalseCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				}//市局
				this.reportsService.update(reports);
			//	mv.setViewName(this.viewName.concat("2"));
		    // 清理文章缓存
		    NginxPurgeCacheUtil.purge(2, reports.getId());
			}
			// 清理栏目列表缓存
		
		} catch(Exception e) {
		//	mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "新闻保存异常！", e);
		}

		return mv;
	}
}
